﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Text;
using System.IO;

namespace AnaViewer
{
    [Serializable]
    public class ThemeData
    {
        //public string ThemeName = "默认";
        public Color ForeColor = Color.Black;
        public Color BackColor = Color.White;
        public Color HoverColor = Color.FromArgb(255, 199, 60);// Color.Gold;
        public Font TitleFont = Control.DefaultFont;
        public Font TimeFont = Control.DefaultFont;
        public Font AuthorFont = Control.DefaultFont;
        public bool AuthorVisible = true;

        public HoverMode FaceEffect = HoverMode.BlankAndWhite;

        public ThemeData()
        {
        }
        public ThemeData(string themeName,Color foreColor, Color backColor, Color hoverColor, Font font, bool authorVisible)
        {
            //this.ThemeName = themeName;
            this.ForeColor = foreColor;
            this.BackColor = backColor;
            this.HoverColor = hoverColor;
            this.TitleFont = font;
            this.AuthorVisible = authorVisible;
        }
    }

    internal class ThemeHelper
    {
        static ThemeHelper instance = null;
        public static ThemeHelper Default
        {
            get
            {
                if (instance == null)
                    instance = new ThemeHelper();

                return instance;
            }
        }
        private ThemeHelper()
        {
            LoadThemes();
            if (dicThemes == null)
                LoadDefaultThemes();
        }

        private Dictionary<string, ThemeData> dicThemes;
        /// <summary>
        /// 支持的风格
        /// </summary>
        public IList<string> SupportThemeNames
        {
            get
            {
                List<string> items = new List<string>();
                foreach (string name in dicThemes.Keys)
                    items.Add(name);
                return items.AsReadOnly();
            }
        }
        public IList<ThemeData> SupportThemes
        {
            get
            {
                List<ThemeData> items = new List<ThemeData>();
                foreach (ThemeData data in dicThemes.Values)
                    items.Add(data);
                return items.AsReadOnly();
            }
        }

        /// <summary>
        /// 内置的风格。 "默认", "古典", "浪漫", "绿茵", "玫瑰红", "自定义"
        /// </summary>
        void LoadDefaultThemes()
        {
            if (dicThemes == null)
                dicThemes = new Dictionary<string, ThemeData>();
            else
                dicThemes.Clear();

            ThemeData theme;

            FontFamily family = null;

            // ---- 默认 ----
            theme = new ThemeData();
            theme.ForeColor = Color.Black;
            theme.BackColor = Color.White;
            theme.HoverColor = Color.Gold;

            family = FontsHelper.GetFamily("方正静蕾简体");
            theme.AuthorFont = new Font(family, 16.0F);//new Font("方正静蕾简体", 16.0F);
            theme.TitleFont = new Font(family, 22.25F);// new Font("方正静蕾简体", 26.25F);
            theme.TimeFont = new Font(family, 18.0F);// new Font("方正静蕾简体", 20.25F);
            theme.FaceEffect = HoverMode.BlankAndWhite;
            dicThemes.Add("默认", theme);

            // ---- 古典 ----
            theme = new ThemeData();
            theme.ForeColor = Color.FromArgb(67, 63, 53);     //深色
            theme.BackColor = Color.FromArgb(224, 218, 186);    //浅色
            theme.HoverColor = Color.Gold;

            family = FontsHelper.GetFamily("方正启体繁体");
            theme.AuthorFont = new Font(family, 16.0F);
            theme.TitleFont = new Font(family, 26.25F);
            theme.TimeFont = new Font(family, 22.25F);
            theme.AuthorVisible = false;
            theme.FaceEffect = HoverMode.CoverColor;
            dicThemes.Add("古典", theme);

            // ---- 浪漫 ----
            theme = new ThemeData();
            theme.ForeColor = Color.FromArgb(160, 0, 160);      //深色 - 紫色
            theme.BackColor = Color.FromArgb(255, 204, 255);  //浅色 - 粉红
            theme.HoverColor = Color.FromArgb(255, 90, 255);

            family = FontsHelper.GetFamily("华康少女文字W5(P)");
            theme.AuthorFont = new Font(family, 16.0F);
            theme.TitleFont = new Font(family, 20.25F);
            theme.TimeFont = new Font(family, 18.0F);
            theme.FaceEffect = HoverMode.CoverColor;
            dicThemes.Add("浪漫", theme);

            // ---- 绿茵 ----
            theme = new ThemeData();
            theme.ForeColor = Color.FromArgb(00, 51, 00);     //深色 - 深绿
            theme.BackColor = Color.FromArgb(153, 255, 153);  //浅色 - 浅绿
            theme.HoverColor = Color.FromArgb(00, 200, 00);

            family = FontsHelper.GetFamily("方正静蕾简体");
            theme.AuthorFont = new Font(family, 16.0F);
            theme.TitleFont = new Font(family, 22.25F);
            theme.TimeFont = new Font(family, 18.0F);
            theme.FaceEffect = HoverMode.BlankAndWhite;
            dicThemes.Add("绿茵", theme);

            // ---- 玫瑰红 ----
            theme = new ThemeData();
            //data.ForeColor = Color.FromArgb(7,30,129);      //前景色 - 深蓝
            //data.BackColor = Color.FromArgb(255,70,70);  //背景色 - 玫瑰红
            //toolbarBackColor = Color.FromArgb(255, 195, 195);//工具栏背景色 - 浅红
            //toolbarForeColor = Color.FromArgb(55, 101, 133);//工具栏前景色
            theme.BackColor = Color.FromArgb(255, 195, 195);//工具栏背景色 - 浅红
            theme.ForeColor = Color.FromArgb(55, 101, 133);//工具栏前景色
            theme.HoverColor = Color.FromArgb(255, 70, 70);

            family = FontsHelper.GetFamily("方正静蕾简体");
            theme.AuthorFont = new Font(family, 16.0F);
            theme.TitleFont = new Font(family, 22.25F);
            theme.TimeFont = new Font(family, 18.0F);
            theme.FaceEffect = HoverMode.CoverColor;
            dicThemes.Add("玫瑰红", theme);

            // ---- 自定义 ----
            //theme = customThemeData;
            //    dicThemes.Add("自定义",theme);

        }
        public ThemeData GetThemeData(string themeName)
        {
            if (dicThemes.ContainsKey(themeName))
                return dicThemes[themeName];
            else
            {
                if (dicThemes.ContainsKey("默认"))
                    return dicThemes["默认"];
                else
                    return null;
            }
        }
        /// <summary>
        /// 设置样式，如果指定的样式名称不存在，则添加
        /// </summary>
        /// <param name="themeName"></param>
        /// <param name="data"></param>
        public void SetThemeData(string themeName, ThemeData data)
        {
            if (dicThemes.ContainsKey(themeName))
                dicThemes[themeName] = data;
            else
                dicThemes.Add(themeName, data);

            SaveThemes();
        }
        public void DeleteThemeData(string themeName)
        {
            if (dicThemes.ContainsKey(themeName))
                dicThemes.Remove(themeName);

            SaveThemes();
        }


        /// <summary>
        /// 脱机数据文件名称。DataSetFileName
        /// </summary>
        const string DataFileName = "themes.dat";
        static string ThemeDataFullPath
        {
            get
            {
                //return Path.Combine( Application.UserAppDataPath, DataFileName);
                return Path.Combine(Application.StartupPath, DataFileName);
            }
        }
        void SaveThemes()
        {
            SerializationHelper.Serialize(dicThemes, ThemeDataFullPath);
        }

        void LoadThemes()
        {
            try
            {
                dicThemes = SerializationHelper.Deserialize(ThemeDataFullPath) as Dictionary<string, ThemeData>;
            }
            catch (Exception)
            {
            }
        }

    }
}
